package com.dome.mvp.uiview;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RelativeLayout;

import androidx.core.content.ContextCompat;

import com.dome.mvp.R;
import com.dome.mvp.databinding.LayoutToolBarBinding;
import com.dome.mvp.uiview.listener.ClickCallBackListener;
import com.dome.mvp.utils.ActivityManageUtils;


/**
 * Time: 4/28/2021 14:46
 * Author: TAO
 * Description:
 */
public class UIToolBar extends RelativeLayout {

    private ClickCallBackListener leftListener;
    private ClickCallBackListener rightListener;


    public UIToolBar(Context context) {
        this(context, null);
    }

    public UIToolBar(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public UIToolBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context, attrs);
    }

    private void initView(Context context, AttributeSet attrs) {
        LayoutToolBarBinding binding = LayoutToolBarBinding.inflate(LayoutInflater.from(context), this, true);
        binding.setToolBar(this);
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.UIToolBar);
        // 左侧图片地址
        int leftImg = typedArray.getResourceId(R.styleable.UIToolBar_leftImg, R.mipmap.back_black);
        /// 设置图片
        if (leftImg != -1) {
            Drawable drawable = ContextCompat.getDrawable(context, leftImg);
            if (drawable != null) {
                binding.setLeftImg(drawable);
            }
        }
        // 左侧文字
        String leftText = typedArray.getString(R.styleable.UIToolBar_leftText);
        binding.setLeftText(leftText);

        // 标题文字
        String title = typedArray.getString(R.styleable.UIToolBar_toolTitle);
        binding.setTitle(title);
        // 标题文字大小
        int size = typedArray.getDimensionPixelSize(R.styleable.UIToolBar_toolTitleTextSize, 16);
        binding.setSize(size);
        // 标题颜色
        int color = typedArray.getColor(R.styleable.UIToolBar_toolTitleTextColor, Color.BLACK);
        binding.setTitleTextColor(color);

        // 右侧图片
        int rightImg = typedArray.getResourceId(R.styleable.UIToolBar_leftImg, -1);
        /// 右侧图片
        if (rightImg != -1) {
            Drawable drawable = ContextCompat.getDrawable(context, rightImg);
            if (drawable != null) {
                binding.setRightImg(drawable);
            }
        }
        // 右侧文字
        String rightText = typedArray.getString(R.styleable.UIToolBar_rightText);
        binding.setRightText(rightText);
        typedArray.recycle();
    }

    public void clickLeft(View view) {
        if (leftListener == null) {
            ActivityManageUtils.getInstance().popFinish();
        } else {
            leftListener.clickCallBack();
        }
    }

    public void clickRight(View view) {
        if (rightListener == null) return;
        rightListener.clickCallBack();
    }

    public void setLeftListener(ClickCallBackListener leftListener) {
        this.leftListener = leftListener;
    }

    public void setRightListener(ClickCallBackListener rightListener) {
        this.rightListener = rightListener;
    }
}
